package cc.imos;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.web.servlet.ErrorPage;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.http.HttpStatus;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import org.springframework.web.socket.config.annotation.EnableWebSocket;

import java.util.Map;

@Slf4j // 日志注解
@EnableAsync // 启用异步
@EnableCaching // 启动缓存
@EnableWebSocket // 启用 WebSocket
@EnableScheduling // 启用定时任务的配置
@ServletComponentScan // 启用 Servlet 和 Filter 扫描
@SpringBootApplication // 程序入口
@EnableTransactionManagement // 启用注解事物
public class BasicApplication {

	public static void main(String[] args) {
		ConfigurableApplicationContext configurableApplicationContext = SpringApplication.run(BasicApplication.class, args);
		log.debug("======================================= START =======================================");
		log.debug("项目启动完成：{}" , "https://local.imos.cc:8001/");

//		ServletContext sc = request.getSession().getServletContext();
//		WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
//		WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
		RequestMappingHandlerMapping requestMappingHandlerMapping = configurableApplicationContext.getBean(RequestMappingHandlerMapping.class);
		Map<RequestMappingInfo, HandlerMethod> map = requestMappingHandlerMapping.getHandlerMethods();
		for(RequestMappingInfo info : map.keySet()){
			log.debug("项目请求地址：" + info.getPatternsCondition().toString() + "," + map.get(info).getBean().toString());
		}
	}

//	@Bean
//	public FilterRegistrationBean  filterRegistrationBean() {
//		FilterRegistrationBean registrationBean = new FilterRegistrationBean();
//		BasicFilter bootFilter = new BasicFilter();
//		registrationBean.setFilter(bootFilter);
//		return registrationBean;
//	}

//    @Bean
//    @Order(Ordered.HIGHEST_PRECEDENCE)
//    public CharacterEncodingFilter characterEncodingFilter() {
//        CharacterEncodingFilter filter = new CharacterEncodingFilter("UTF-8", true);
//        return filter;
//    }

//	@Bean
	public EmbeddedServletContainerCustomizer containerCustomizer() {
		return (container -> {
			ErrorPage error401Page = new ErrorPage(HttpStatus.UNAUTHORIZED, "/401.html");
			ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/404.html");
			ErrorPage error500Page = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/500.html");

			container.addErrorPages(error401Page, error404Page, error500Page);
		});
	}

}
